package com.gitee.mars.marsquickframe.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gitee.mars.marsquickframe.entity.AdminOperateLog;
import com.gitee.mars.marsquickframe.exception.WorkException;
import com.gitee.mars.marsquickframe.mapper.AdminOperateLogMapper;
import com.gitee.mars.marsquickframe.mapper.SysUserMapper;
import com.gitee.mars.marsquickframe.request.AdminOperateLogReq;
import com.gitee.mars.marsquickframe.response.base.PageInfo;
import com.gitee.mars.marsquickframe.response.vo.AdminOperateVo;
import com.gitee.mars.marsquickframe.service.IAdminOperateLogService;
import lombok.AllArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;


/**
 * AdminOperateLog 实现层
 *
 * @Author wq
 * @Date 2021-02-03
 **/
@Service
@Transactional(rollbackFor = RuntimeException.class)
@AllArgsConstructor
public class AdminOperateLogServiceImpl extends ServiceImpl<AdminOperateLogMapper, AdminOperateLog> implements IAdminOperateLogService {

    private final SysUserMapper sysUserMapper;

    @Override
    public void add(AdminOperateLogReq req) {
        if (StringUtils.isEmpty(req.getContent())) {
            throw new WorkException("操作日志不能为空");
        }
        AdminOperateLog adminOperateLog = new AdminOperateLog();
        adminOperateLog.setContent(req.getContent());
        adminOperateLog.setCreateTime(new Date());
        adminOperateLog.setSysUserId(req.getSysUserId());
        save(adminOperateLog);
    }

    @Override
    public PageInfo<AdminOperateVo> pageList(AdminOperateLogReq req) {
        IPage<AdminOperateLog> page = new Page<>(req.getPageNumber(), req.getPageSize());
        LambdaQueryWrapper<AdminOperateLog> wrapper = new LambdaQueryWrapper<>();
        if (StringUtils.isNotEmpty(req.getContent())) {
            wrapper.like(AdminOperateLog::getContent, req.getContent());
        }
        if (Objects.nonNull(req.getSysUserId())) {
            wrapper.eq(AdminOperateLog::getSysUserId, req.getSysUserId());
        }
        wrapper.orderByDesc(AdminOperateLog::getCreateTime);
        List<AdminOperateVo> list = baseMapper.selectPage(page, wrapper).getRecords().stream().map(x -> {
            AdminOperateVo vo = new AdminOperateVo();
            vo.setId(x.getId());
            vo.setContent(x.getContent());
            vo.setNickname(sysUserMapper.selectNicknameByUserId(x.getSysUserId()));
            vo.setSysUserId(x.getSysUserId());
            vo.setCreateTime(x.getCreateTime());
            return vo;
        }).collect(Collectors.toList());
        if (StringUtils.isNotEmpty(req.getNickname())) {
            List<AdminOperateVo> operateVoList = list.stream().filter(adminOperateVo -> adminOperateVo.getNickname().
                    contains(req.getNickname())).collect(Collectors.toList());
            return new PageInfo<>((long) operateVoList.size(), operateVoList);
        }
        return new PageInfo<>(page.getTotal(), list);
    }
}